<?php

define ("KB_SIZE", 1024);
define ("MB_SIZE", 1048576);
define ("GB_SIZE", 1073741824);
define ("TB_SIZE", 1099511627776);

function human_readable_size ($s, $unit) {
	if ( !$s )
		return "?";
	
	$s = intval($s / $unit) . "." . substr((floatval($s / $unit) - intval($s / $unit)), 2, 2);
	switch ($unit) {
		case KB_SIZE: $s .= " KB";
			break;
		case MB_SIZE: $s .= " MB";
			break;
		case GB_SIZE: $s .= " GB";
			break;
		case TB_SIZE: $s .= " TB";
			break;
		default:
			return "Invalid unit.";
	}
	
	return $s;
}

?>
